package com.itcast.googleplay.utils;

import com.itcast.googleplay.application.MyApplication;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.View;

public class UIUtils {
	public static Context getContext(){
		return MyApplication.getContext();
	}
	public static Thread getMainThread(){
		return MyApplication.getMainThread();
	}
	public static int getMainThreadId(){
		return MyApplication.getMainThreadId();
	}
	public static Handler getHandler(){
		return MyApplication.getHandler();
	}
	public static int dip2px(int dip){
		float density = getContext().getResources().getDisplayMetrics().density;
		return (int)(density*dip+0.5f);
	}
	
	public static int px2dip(int px){
		float density = getContext().getResources().getDisplayMetrics().density;
		return (int)(px/density+0.5f);
	}
	
	public static Resources getResources(){
		return getContext().getResources();
	}
	
	public static Drawable getDrawable(int drawableId){
		return getResources().getDrawable(drawableId);
	}
	
	public static void runInMainThread(Runnable runnable){
		if(getMainThreadId() == android.os.Process.myTid()){
			//主线程
			runnable.run();
		}else{
			//不是主线程,通过handler发送到主线程中运行
			getHandler().post(runnable);
		}
	}
	public static ColorStateList getColorStateList(int mTabTextColorResId) {
		return getResources().getColorStateList(mTabTextColorResId);
	}
	public static String[] getStringArray(int tabIds) {
		return getResources().getStringArray(tabIds);
	}
	
	public static View inflate(int layoutId){
		return View.inflate(getContext(), layoutId, null);
	}
	public static boolean isRunInMainThread() {
		if(getMainThreadId() == android.os.Process.myTid()){
			return true;
		}else{
			return false;
		}
	}
	public static int getColor(int color) {
		return getResources().getColor(color);
	}
}
